layui.define(['jquery', 'kaBgUrl', "util", "kaPreviewText"],
    function (exports) {
        var $ = layui.jquery;
        var kaBgUrl = layui.kaBgUrl;
        var util = layui.util;
        var kaPreviewText = layui.kaPreviewText;
        var kaLog = new function () {
            this.kaLogId = "kaLogId";
            // 打印
            this.print = function (logType, log, noLog) {
                if (top.kaLogBoolean != 0) { // 错误信号
                    //0:不打印 1：打印， 2：打印所有信息并保存后台， 3：打印所有信息并错误信息保存后台，4：打印错误信息并错误信息保存后台
                    var logstr = "";
                    for (var j in log) {
                        if (typeof log[j] === "object" && log[j] !== null) {
                            var cache = [];
                            logstr = logstr + "\r\n" + JSON.stringify(log[j], function (key, value) {
                                if (typeof value === 'object' && value !== null) {
                                    if (cache.indexOf(value) !== -1) {
                                        return;
                                    }
                                    cache.push(value);
                                }
                                return value;
                            }, '\t');
                            cache = null;	//释放cache
                        } else {
                            logstr = logstr + " " + log[j];
                        }
                    }
                    var myDate = util.toDateString(new Date().getTime(), 'yyyy-MM-dd HH:mm:ss');
                    var model_logs_html = '\r\n[' + myDate + '] ' + logstr;
                    if (top.kaLogBoolean == 1) {
                        if (noLog == undefined) {
                            console.log(model_logs_html);
                            kaPreviewText.set(kaLog.kaLogId, model_logs_html, 1);
                        }
                    } else if (top.kaLogBoolean == 2) {
                        if (noLog == undefined) {
                            console.log(model_logs_html);
                        }
                        //所有信息并保存后台
                        $.ajax({
                            url: kaBgUrl.url(kaLogUrl),
                            type: "POST",
                            data: {},
                            dataType: "json",
                            contentType: "application/json;charset=UTF-8",
                            success: function (data) {
                                console.log("后台 id:" + data.id);
                                kaPreviewText.set(kaLog.kaLogId, "后台 id:" + data.id, 1);
                            },
                            error: function () { }
                        });
                    } else if (top.kaLogBoolean == 3) {
                        if (noLog == undefined) {
                            console.log(model_logs_html);
                            kaPreviewText.set(kaLog.kaLogId, model_logs_html, 1);
                        }
                        //错误信息保存后台
                        if (logType == "error") {
                            $.ajax({
                                url: kaBgUrl.url(kaLogUrl),
                                type: "POST",
                                data: {},
                                dataType: "json",
                                contentType: "application/json;charset=UTF-8",
                                success: function (data) {
                                    console.log("后台 id:" + data.id);
                                    kaPreviewText.set(kaLog.kaLogId, "后台 id:" + data.id, 1);
                                },
                                error: function () { }
                            });
                        }
                    } else if (top.kaLogBoolean == 4) {
                        if (logType == "error") {
                            if (noLog == undefined) {
                                console.log(model_logs_html);
                                kaPreviewText.set(kaLog.kaLogId, model_logs_html, 1);
                            }
                            //错误信息保存后台
                            $.ajax({
                                url: kaBgUrl.url(kaLogUrl),
                                type: "POST",
                                data: {},
                                dataType: "json",
                                contentType: "application/json;charset=UTF-8",
                                success: function (data) {
                                    console.log("后台 id:" + data.id);
                                    kaPreviewText.set(kaLog.kaLogId, "后台 id:" + data.id, 1);
                                },
                                error: function () { }
                            });
                        }
                    }
                }
            }
            this.openLogView = function () {//打开日志监控
                kaPreviewText.open(this.kaLogId);
            }
            this.clearLog = function () { //清除日志

            }
            this.downloadLogs = function () { // 生成下载日志文件

            }
        };
        exports('kaLog', kaLog);
    }
)